﻿using Inovout.Memberships;
using Inovout.Memberships.Models;
using Inovout.Organizations.Memberships.Models;
using Inovout.Organizations.Memberships.Repositories;
using Inovout.Organizations.Models;
using Inovout.Security;
using Inovout.Security.Services;
using Inovout.Services;

namespace Inovout.Organizations.Memberships.Services
{
    public class OrganizationMembershipService : RepositoryService<OrganizationMembership>, IOrganizationMembershipService
    {
        protected OrganizationMembershipRepository OrganizationMembershipRepository
        {
            get { return (OrganizationMembershipRepository)base.Repository; }
        }
        private IFormsUserCredentialService formUserCredentialService;
        private IRepositoryService<OpenIdUserCredential> openIdUserCredentialService;
        private IRepositoryService<OAuthUserCredential> oAuthUserCredentialService;
        private IRepositoryService<OrganizationUnit> organizationUnitService;
        private IRepositoryService<MembershipUser> membershipUserService;
        private IRoleService roleService;
        public OrganizationMembershipService(IRepositoryService<OrganizationUnit> organizationUnitService,
            IFormsUserCredentialService formUserCredentialService, IRepositoryService<MembershipUser> membershipUserService,
            IRoleService roleService,
            IRepositoryService<OpenIdUserCredential> openIdUserCredentialService,
            IRepositoryService<OAuthUserCredential> oAuthUserCredentialService)
        {
            this.organizationUnitService = organizationUnitService;
            this.formUserCredentialService = formUserCredentialService;
            this.membershipUserService = membershipUserService;
            this.roleService = roleService;
            this.openIdUserCredentialService = openIdUserCredentialService;
            this.oAuthUserCredentialService = oAuthUserCredentialService;
        }
        public IMembershipUser Create(OrganizationUnit organizationUnit, UserCredential useCredential, MembershipUserStatus status)
        {
            OrganizationMembership organizationMembership = null;
         
            if (useCredential is OpenIdUserCredential)
            {
                openIdUserCredentialService.Save((OpenIdUserCredential)useCredential);
             }
            if (useCredential is FormsUserCredential)
            {
                formUserCredentialService.Create((FormsUserCredential)useCredential);
                
            }
            if (useCredential is OAuthUserCredential)
            {
                oAuthUserCredentialService.Save((OAuthUserCredential)useCredential);
            }


            organizationMembership = new OrganizationMembership(organizationUnit);
            base.Save(organizationMembership);
            var membershipUser = new MembershipUser(organizationMembership, useCredential.SystemUser, organizationUnit.Email, status);
            membershipUserService.Save(membershipUser);

            if (!Application.Settings.Contains("OrganizationMembershipDefaultRoles"))
            {
                roleService.AddUserToRoles(useCredential.SystemUser, Application.Settings["OrganizationMembershipDefaultRoles"].ToString().Split(','));
            }

            return membershipUser;
        }


        public OrganizationMembership FindByUserName(string userName)
        {
            return OrganizationMembershipRepository.FindByUserName(userName);
        }

        public OrganizationMembership FindByOrganizationUnit(OrganizationUnit organizationUnit)
        {
            return OrganizationMembershipRepository.FindByOrganizationUnit(organizationUnit);
        }
    }
}
